//
//  MessageViewController.m
//  Civacrm
//
//  Created by zhk on 2017/2/7.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"

static NSString * MyMessageTableViewCellReuseID = @"MessageCell";

@interface MessageViewController ()

@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) UITableView * messageTableView;

@property (nonatomic , assign) NSInteger index;

@property (nonatomic , assign) NSInteger pageNum;

@end

@implementation MessageViewController

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageNum = 0;
    [self initNavigation];
    [self initView];
    [self.messageTableView.mj_header beginRefreshing];
}

- (void)initNetWork
{
    self.params[@"userId"] = self.userID;
    self.params[@"messageType"] = @"0";
    self.params[@"pageNo"] = [NSString stringWithFormat:@"%ld",self.pageNum];
    self.params[@"pageSize"] = @"10";
    [self.manager postWithURL:NET_GetMessage param:self.params delegate:self isEncrypt:NO type:NetWorkingType0];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type
{
    
}

- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type
{
    
    // 如果是第一页 清空数组 再往数组添加 页码加一 停止刷新
    [self endRefresh];
    if (self.pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    self.pageNum ++;
    NSArray * array = [responseObject[@"content"] objectForKey:@"messageList"];
    
    for ( int i = 0; i < array.count; i ++) {
        MessageModel * model = [[MessageModel alloc]init];
        [model setValuesForKeysWithDictionary:array[i]];
        model.isOpen = NO;
        model.labelHeight = [model heightAccordingText:model.content];
        [self.dataArray addObject:model];
    }
    
    if (self.dataArray.count < [self.params[@"pageSize"] integerValue]) {
        [self.messageTableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.messageTableView reloadData];
    
    
    if (array.count == 0) {
        self.nillView = [[NillView alloc] initViewWithType:NillViewType0 frame:self.view.bounds];
        [self.view addSubview:self.nillView];
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.beAbleToOpen) {
        if (indexPath.section == self.index) {
            MessageModel * model = self.dataArray[self.index];
            model.isOpen = !model.isOpen;
            self.index = indexPath.section;
            [self.messageTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            return;
        }
        for (MessageModel * model in self.dataArray) {
            model.isOpen = NO;
        }
        MessageModel * clickModel = self.dataArray[indexPath.section];
        clickModel.isOpen = !clickModel.isOpen;
        
        
        self.index = indexPath.section;
        [self.messageTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    return;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel * model = self.dataArray[indexPath.section];
    if (model.isOpen) {
        return 70 + model.labelHeight;
    }
    
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MyMessageTableViewCellReuseID forIndexPath:indexPath];
    
    if (self.dataArray.count > indexPath.section) {
        MessageModel * model = self.dataArray[indexPath.section];
        [cell takeDataForCellWithModel:model];
        if (model.labelHeight > 20) {
            cell.beAbleToOpen = YES;
        }
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (void)initView
{
    self.view.backgroundColor = GlobalBgColor;
    
    self.messageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 ) style:(UITableViewStyleGrouped)];
    self.messageTableView.backgroundColor = GlobalBgColor;
    [self.messageTableView registerClass:[MessageTableViewCell class] forCellReuseIdentifier:MyMessageTableViewCellReuseID];
    [self.view addSubview:self.messageTableView];
    self.messageTableView.delegate = self;
    self.messageTableView.dataSource = self;
    self.messageTableView.tableFooterView = [[UIView alloc]init];
    self.messageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.messageTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleHeaderRefreshingAction)];
    self.messageTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleFooterRefreshingAction)];
}

- (void)initNavigation
{
    self.title = @"消息";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handleHeaderRefreshingAction
{
    self.pageNum = 1;
    [self initNetWork];
}

- (void)handleFooterRefreshingAction
{
    [self initNetWork];
}

- (void)endRefresh
{
    [self.messageTableView.mj_header endRefreshing];
    [self.messageTableView.mj_footer endRefreshing];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
